import React from 'react';
import { withRouter,useLocation,useHistory } from 'react-router-dom';
import { Button, Input, Form ,Dialog} from 'react-vant'
import { editpass } from '../../../api/userinfo';
const Editpass = (props) => {
    const [form] = Form.useForm()
    const location = useLocation()
    const history = useHistory()
    const onFinish = async values => {
        if(values.password !== values.confirmPass){
          Dialog.alert({
            message:'密码和确认密码不一致'
          })
        }else{
          let res = await editpass({phone:location.state.phone,password:values.password})
          if(res.code === 200){
            history.push('/Loginpass')
          }else{
            Dialog.alert({
              message:'新密码不能和旧密码相同',
              closeOnPopstate:false
            })
          }

        }
    }
    return (
        <Form
      form={form}
      onFinish={onFinish}
      style={{ margin: '200rem 16rem 0' }}
      footer={
        <div style={{ margin: '30rem 16rem 0' }}>
          <Button round nativeType='submit' type='primary' block>
            提交
          </Button>
        </div>
      }
    >
      <Form.Item
        tooltip={{
          message:
            'A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself.',
        }}
        rules={[{ required: true, message: '输入密码' }]}
        name='password'
        label='新的密码'
      >
        <Input placeholder='输入密码' />
      </Form.Item>
      <Form.Item
        rules={[{ required: true, message: '确认密码' }]}
        name='confirmPass'
        label='确认密码'
      >
        <Input placeholder='确认密码' />
      </Form.Item>
    </Form>
    );
}

export default withRouter(Editpass);
